export function makeThrower (promiseIn: Promise<any>) {
  const checkStatus = getStatusChecker(promiseIn);
  return function () {
    const {promise, status, result} = checkStatus();
    if (status === "pending") throw promise;
    if (status === "error") throw result;
    return result;
  };
}

export function getStatusChecker (promiseIn: Promise<any>) {
  let status = "pending";
  let result: any;
  const promise = promiseIn.then((response) => {
    status = "success";
    result = response;
  }).catch((error) => {
    status = "error";
    result = error;
  });
  return () => ({promise, status, result});
}

export function fetchMessage() {
  return new Promise<any>((resolve, reject) => {
    window.setTimeout(() => {
      const rnd = Math.floor(Math.random() * 10 + 1)
      resolve({ message: rnd })
    }, 3000)
  })
}

export function fetchNextMessage() {
  return new Promise<any>((resolve, reject) => {
    window.setTimeout(() => {
      const rnd = Math.floor(Math.random() * 10 + 1)
      resolve({ message: rnd })
    }, 1000)
  })
}